<template>
  <div class="login-container">
    <div class="loginForm">
      <div class="avatar">
        <img :src="avatar" alt="">
      </div>
      <el-form :model="loginForm"  :rules="loginFormRules"  ref="ruleForm" class="login-form">
        <el-form-item prop="username">
          <el-input type="input" v-model="loginForm.username" prefix-icon="el-icon-user-solid"  autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-lock" suffix-icon="el-icon-view" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import logo from '@/assets/logo.png'
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      avatar: logo,
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.$router.options.routes[2].children.length = 0
    console.log('login', this.$router.options.routes[2].children.length)
  },
  methods: {
    ...mapMutations('user', ['changeState']),
    submitForm () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.login()
        } else {
          console.log('error submit!!')
          this.$message.error('请输入正确后再提交')
          return false
        }
      })
    },
    async login () {
      const result = await this.$http.post('/login', this.loginForm)
      if (result.data.code) {
        this.$message.success('登录成功')
        console.log(result.data.data)
        this.changeState(result.data.data)
        this.$router.push('/home')
        return
      }
      this.$message.error('账号或密码有误')
    }
  }
}
</script>

<style lang="less" scoped>
  .login-container{
    width: 100%;
    height: 100%;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #000;
    .loginForm{
      width: 750px;
      height: 450px;
      background: #fff;
      border-radius: 35px;
      position: relative;
      box-shadow: 0px 0px 38px -4px #fff;
      .avatar{
        width: 250px;
        height: 250px;
        overflow: hidden;
        border-radius:50%;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        img{
          width: 100%;
        }
      }
      .login-form{
        margin: 50% auto;
        transform: translateY(-125%);
        width: 450px;
      }
    }
  }
</style>
